package com.axzhengxin.platform.business.service.impl;

import com.axzhengxin.platform.business.domain.BizApproveDetail;
import com.axzhengxin.platform.business.mapper.BizApproveDetailMapper;
import com.axzhengxin.platform.business.mapper.BizApproveInfoMapper;
import com.axzhengxin.platform.business.service.IBizApproveDetailService;
import com.axzhengxin.platform.common.util.IdUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author AHZJ0005
 * @Date 2021/3/3-10:11
 **/
@Slf4j
@Service
public class BizApproveDetailServiceImpl implements IBizApproveDetailService {

    @Autowired
    private BizApproveDetailMapper bizApproveDetailMapper;
    @Autowired
    private BizApproveInfoMapper bizApproveInfoMapper;
    @Autowired
    private IdUtils idUtils;


    @Override
    public List<BizApproveDetail> bizApproveDetailList(String approveId) {
        return bizApproveDetailMapper.bizApproveDetailList(approveId);
    }

    @Override
    public int deleteById(String approveDetailId) {
        //删除的同时修改授信金额
        BizApproveDetail bizApproveDetail = bizApproveDetailMapper.selectBizApproveDetailById(approveDetailId);
        int i = bizApproveDetailMapper.deleteBizApproveDetailById(approveDetailId);
        String approveId = bizApproveDetail.getApproveId();
        BigDecimal approveAmount = bizApproveDetail.getApproveAmount();
        if (i > 0) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("approveId", approveId);
            map1.put("amount", approveAmount);
            bizApproveInfoMapper.updateAmountByApproveIdPlus(map1);
        }

        return i;
    }

    /**
     * 添加之前判断授信总额是否超过融资总额
     *
     * @param bizApproveDetail
     * @return
     */
    @Override
    public AjaxResult insertApproveDetail(BizApproveDetail bizApproveDetail) {
        BigDecimal approveAmount = bizApproveDetail.getApproveAmount();
        if (BigDecimal.ZERO.equals(approveAmount)){
            return AjaxResult.error("金额不可设置为0");
        }
        String approveId = bizApproveDetail.getApproveId();
        HashMap<String, Object> map = new HashMap<>();
        map.put("approveAmount", approveAmount);
        map.put("approveId", approveId);
        /*Integer result = bizApproveDetailMapper.checkApproveAmountByReqId(map);
        if (result!=null&&result==-1){*/
        String snakeId = idUtils.getSnakeId();
        /*log.info(snakeId+"--------------------------------");*/
        bizApproveDetail.setApproveDetailId(snakeId);
        bizApproveDetailMapper.insertBizApproveDetail(bizApproveDetail);
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("approveId", approveId);
        map1.put("amount", approveAmount);
        bizApproveInfoMapper.updateAmountByApproveId(map1);
        return AjaxResult.success("新增成功");
    /*}else {
            return AjaxResult.error("超过当前可进行的授信额度，当前剩余可授信"+result+"万元");
        }*/
    }
}
